package br.com.maps.data;

import java.net.URL;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import br.com.maps.contacts.Contact;
import br.com.maps.contacts.database.DataBaseFetcher;
import br.com.maps.database.DBVersionHandlerMock;

public class DataFetcher {

	private ProgressDialog progressDialog;
	private Context context;
	private DataObtainer dataObtainer;

	public DataFetcher(Context context, DataObtainer dataObtainer) {
		this.context = context;
		this.dataObtainer = dataObtainer;
	}

	public void execute(URL... params) {
		if (params.length > 1) {
			throw new IllegalArgumentException("Accepts only one url");
		}
		this.onPreExecute();
		List<Contact> result = this.dataObtainer.obtainData(params[0]);
		this.onPostExecute(result);
	}

	protected void onPreExecute() {
		this.progressDialog = new ProgressDialog(this.context);
		this.progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		this.progressDialog.setMessage("Downloading data...");
		this.progressDialog.show();
	}

	@SuppressWarnings("unchecked")
	protected void onPostExecute(List<Contact> result) {
		this.progressDialog.dismiss();
		new DataBaseFetcher(this.context, new DBVersionHandlerMock()).execute(result);
	}

}
